package org.yylnb.openplatform.payment.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yylnb.openplatform.payment.utlis.PayCommonUtil;
import org.yylnb.openplatform.payment.utlis.ZxingUtil;

import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @author RainLin
 * @date 2020/7/28 - 20:34
 */
@Controller
public class PayMentController {

    @RequestMapping("/pay")
    public String pay(String orderId, String title,  HttpServletRequest request) {
        try {
            int i = new Random().nextInt(1);
            String wxpayURL = PayCommonUtil.weixin_pay("1"+i, title, orderId);
            BufferedImage image = ZxingUtil.createImage(wxpayURL, 350, 350);
            request.setAttribute("oid",orderId);
            request.setAttribute("title",title);
            // ImageIO.write(image, "JPEG", response.getOutputStream());
            request.getServletContext().setAttribute(orderId, image);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "payment";
    }

    public static void main(String[] args) {
        try {
            String s = PayCommonUtil.weixin_pay("1", "测试的内容", "DSFSDFEWSDG345534ds");
            System.err.println(s);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

